package com.primb.bench.common.viewmodel;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.primb.bench.R;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * Created by Chen on 2017/5/10.
 * 功能描述：键值对类型数据驱动模型
 */

public class ViewKeyValueEntity implements Parcelable {
    String key;
    String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void addView(Context context, ViewGroup viewGroup) {
        AutoRelativeLayout layout = new AutoRelativeLayout(context);
        TextView tvKey = new TextView(context);
        tvKey.setId(R.id.tv_key_model);
        tvKey.setGravity(Gravity.CENTER);
        tvKey.setTextSize(15);
        tvKey.setTextColor(ContextCompat.getColor(context, R.color.gray_444444));
        if (key != null)
            tvKey.setText(key);
        AutoRelativeLayout.LayoutParams paramsLeft = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsLeft.addRule(AutoRelativeLayout.ALIGN_PARENT_LEFT);
        paramsLeft.addRule(AutoRelativeLayout.CENTER_VERTICAL);
        tvKey.setPadding(50, 30, 0, 30);
        tvKey.setLayoutParams(paramsLeft);
        layout.addView(tvKey);

        TextView tvValue = new TextView(context);
        tvValue.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        tvValue.setTextSize(14);
        tvValue.setTextColor(ContextCompat.getColor(context, R.color.gray_444444));
        if (value != null) {
            tvValue.setText(value);
        }
        AutoRelativeLayout.LayoutParams paramsRight = new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsRight.addRule(AutoRelativeLayout.ALIGN_PARENT_RIGHT);
        paramsRight.addRule(AutoRelativeLayout.RIGHT_OF, tvKey.getId());
        paramsRight.leftMargin = 100;
//        paramsRight.addRule(AutoRelativeLayout.CENTER_VERTICAL);
        tvValue.setLayoutParams(paramsRight);
        tvValue.setPadding(0, 30, 50, 30);
//        tvValue.setGravity(Gravity.LEFT);
//        tvValue.setMaxLines(1);
//        tvValue.setEllipsize(TextUtils.TruncateAt.END);
        layout.addView(tvValue);
        viewGroup.addView(layout);
        View line = new View(context);
        line.setBackgroundColor(ContextCompat.getColor(context, R.color.app_main_gray));
        viewGroup.addView(line, new AutoRelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.key);
        dest.writeString(this.value);
    }

    public ViewKeyValueEntity() {
    }

    public ViewKeyValueEntity(String key, String value) {
        this.key = key;
        this.value = value;
    }

    protected ViewKeyValueEntity(Parcel in) {
        this.key = in.readString();
        this.value = in.readString();
    }

    public static final Creator<ViewKeyValueEntity> CREATOR = new Creator<ViewKeyValueEntity>() {
        @Override
        public ViewKeyValueEntity createFromParcel(Parcel source) {
            return new ViewKeyValueEntity(source);
        }

        @Override
        public ViewKeyValueEntity[] newArray(int size) {
            return new ViewKeyValueEntity[size];
        }
    };
}
